/**
 * Copyright (c) 2011 Basil Shikin, VintageRadio Project
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.vintagephone.view.flat;

import java.util.List;

import org.vintagephone.R;
import org.vintagephone.model.RadioControlListener;
import org.vintagephone.model.Song;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.View;

@SuppressWarnings("unchecked")
public class RadioDialController 
{
	public static final int SCREEN_COUNT = 4;
	private static final String Tag = "RadioDialController";
	
	// Parent objects
	final Context m_context;
	
	
	// Controlled bojecxts
	private final RadioDial m_view;
	
	
	// State objects
	private int m_activeScreen = 0;
	private int[] m_activeSong = new int[ SCREEN_COUNT ];
	private List<Song>[] m_screenSongs = new List[ SCREEN_COUNT ];
	
	private RadioControlListener m_listener;
    private int m_volume;
    private boolean m_isOff;

	
	
	public RadioDialController(Context context) 
	{
		m_context = context;
				
		m_view = new RadioDial( this, context );
	}
	
	/**
	 * Attach a listener for application UI events
	 * 
	 * @param listener Listener to attach
	 */
	public void setListener(RadioControlListener listener) 
	{
		m_listener = listener;
	}
	
	public void initialize() 
	{
		
	}

	public View getView()
	{
		return m_view;
				
	}

    public int getVolume()
    {
        return m_volume;
    }

    public boolean getIsOff()
    {
        return m_isOff;
    }
    
	public void setVolume(int volume)
    {
        m_volume = volume;
        
        m_view.postInvalidate();
    }

    public void setOff(boolean isOff)
    {
        m_isOff = isOff;
        
        m_view.postInvalidate();
    }

    public void setScreenSongs( final int screen, final List<Song> songs )
	{
		m_screenSongs[ screen ] = songs;
	}
	
	public void switchToSong( final Song song)
	{
		final int screen = m_activeScreen;
		final int songNumber = m_screenSongs[ screen ].indexOf( song );
		
		if ( songNumber >= 0 )
		{
    		m_activeSong[screen] = songNumber;
    		
    		m_view.post(new Runnable() {
    	        public void run() 
    	        {
    	        	m_view.showSong( m_activeSong[m_activeScreen], m_activeScreen );
    	        }
    		} );
		}
	}
	
	public void switchToScreen(int screen) 
	{
		Log.d( Tag, "Showing screen " + screen );
		
		m_activeScreen = screen;
		m_view.post(new Runnable() {
	        public void run() 
	        {
	        	m_view.showScreen( m_activeScreen );    	
	        }
		} );
	}
	
	
	
	void onSwipeLeft() 
	{
		Log.d( Tag, "Handling left swipe..." );
		
		final int newFolder  = m_activeScreen - 1;
		if ( newFolder >= 0 )
		{
			if ( m_listener  != null ) m_listener.folderChanged( newFolder );
		}
	}

	void onSwipeRight() 
	{
		Log.d( Tag, "Handling right swipe..." );
		
		final int newFolder  = m_activeScreen + 1;
		if ( newFolder < SCREEN_COUNT )
		{
			if ( m_listener  != null ) m_listener.folderChanged( newFolder );
		}		
	}
	
	void onSwipeUp() 
	{
		Log.d( Tag, "Handling swipe up..." );
		
		if ( m_listener  != null ) m_listener.songChanged( true );
				
	}

	void onSwipeDown() 
	{
		Log.d( Tag, "Handling swipe down..." );
		
		if ( m_listener  != null ) m_listener.songChanged( false );
	}

	String getScreenTitle(int screen) 
	{
		final Resources resources = m_context.getResources();
		switch ( screen) 
		{
			case 0: return resources.getString( R.string.folder_one_name );
			case 1: return resources.getString( R.string.folder_two_name );
			case 2: return resources.getString( R.string.folder_three_name );
			case 3: return resources.getString( R.string.folder_four_name );
			
			default: throw new IllegalArgumentException("Unknown screen: " + screen );
		}
	}

	List<Song> getScreenSongs( int screen) 
	{
		return m_screenSongs[ screen ];
	}

	int getActiveSong(int screen) 
	{
		return m_activeSong[screen];
	}
}
